using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Windows.Forms.Design;

namespace OpenComposite.EII.Designer.Workflow
{
	public partial class FormWorkflowProperties : DockContent
	{
		#region Initialization
		public FormWorkflowProperties()
		{
			InitializeComponent();

			//propertyGridWF.PropertyTabs[0].Clear(PropertyTabScope.Component);
			//propertyGridWF.PropertyTabs.AddTabType(typeof(System.Windows.Forms.PropertyGridInternal.PropertiesTab));
			//propertyGridWF.PropertyTabs.AddTabType(typeof(EventsTab), PropertyTabScope.Component);
		}
		#endregion

		#region Public Members

		#region Properties
		public PropertyGrid PropertyGrid
		{
			get { return propertyGridWF; }
		}
		#endregion

		#region Methods
		public void InitializePropertyGrid(ISite site)
		{
			this.propertyGridWF = new OpenComposite.Base.Controls.PropertyGridEx();
			this.propertyGridWF.Dock = System.Windows.Forms.DockStyle.Fill;
			this.propertyGridWF.Location = new System.Drawing.Point(0, 0);
			this.propertyGridWF.Name = "propertyGridWF";
			this.propertyGridWF.Size = new System.Drawing.Size(260, 271);
			this.propertyGridWF.TabIndex = 0;
			this.propertyGridWF.Site = site;
			this.Controls.Add(this.propertyGridWF);
		}
		#endregion

		#endregion

		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				if ( !isRegistered && value != null ) {
					RepositorySurfaceManager rsm = value.GetService(typeof(RepositorySurfaceManager)) as RepositorySurfaceManager;
					rsm.ActiveDesignSurfaceChanged += new System.ComponentModel.Design.ActiveDesignSurfaceChangedEventHandler(rsm_ActiveDesignSurfaceChanged);
					isRegistered = true;
				}
				base.Site = value;
			}
		}

		void rsm_ActiveDesignSurfaceChanged(object sender, System.ComponentModel.Design.ActiveDesignSurfaceChangedEventArgs e)
		{
			if ( e.NewSurface == null ) return;
			System.ComponentModel.Design.IDesignerHost host = e.NewSurface.ComponentContainer as System.ComponentModel.Design.IDesignerHost;
			if ( host != null && host.RootComponent != null ) {
				this.Site = host.RootComponent.Site;
			}
			if ( e.NewSurface.ComponentContainer.Components.Count > 0 ) {
				System.ComponentModel.Design.ISelectionService selectionService = GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService;
				if ( selectionService == null ) return;
				selectionService.SetSelectedComponents(new IComponent[] { e.NewSurface.ComponentContainer.Components[0] });
			}
		}

		private void FormWorkflowProperties_Deactivate(object sender, EventArgs e)
		{

		}

		private void FormWorkflowProperties_Leave(object sender, EventArgs e)
		{

		}

		private void FormWorkflowProperties_Activated(object sender, EventArgs e)
		{
			this.propertyGridWF.Focus();
		}
		private bool isRegistered = false;

		private void propertyGridWF_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
		}

		private void FormWorkflowProperties_KeyDown(object sender, KeyEventArgs e)
		{
			//if ( ( propertyGridWF.SelectedGridItem.Label == "(Name)" && propertyGridWF.SelectedGridItem.Label == "Name" ) ||
			//    !Helpers.IsValidCharForName((char)e.KeyValue) ) e.Handled = true;
		}

		private void FormWorkflowProperties_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( propertyGridWF == null || propertyGridWF.SelectedGridItem == null ) return;
			if ( ( propertyGridWF.SelectedGridItem.Label == "(Name)" ||
				   propertyGridWF.SelectedGridItem.Label == "Name" ||
				   propertyGridWF.SelectedGridItem.Label == "OutputVarName" ) &&
				!Helpers.IsValidCharForName(e.KeyChar) ) e.Handled = true;
		}

		private void FormWorkflowProperties_KeyUp(object sender, KeyEventArgs e)
		{
			if ( e.Alt && e.Control && e.KeyCode == Keys.I ) {
				if ( e.Shift ) {
					propertyGridWF.SelectedObject.OpenDebugViewer();
				} else {
					propertyGridWF.SelectedGridItem.Value.OpenDebugViewer();
				}
			}
		}
	}
}